home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / toolkit.jar / content / global / selectDialog.js < prev    next >
Encoding:
JavaScript  |  2001-03-21  |  4.0 KB  |  134 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape Communications
  16.  * Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. var elements = [];
  25. var numItems;
  26. var list;
  27.  
  28. function selectDialogOnLoad() {
  29.   doSetOKCancel( commonDialogOnOK, commonDialogOnCancel );
  30.  
  31.   param = window.arguments[0].QueryInterface( Components.interfaces.nsIDialogParamBlock  );
  32.   if( !param )
  33.   dump( " error getting param block interface\n" );
  34.  
  35.   var messageText = param.GetString( 1 );
  36.   {
  37.     var messageFragment;
  38.  
  39.     // Let the caller use "\n" to cause breaks
  40.     // Translate these into <br> tags
  41.     var messageParent = (document.getElementById("info.txt"));
  42.     done = false;
  43.     while (!done) {
  44.     breakIndex =   messageText.indexOf('\n');
  45.       if (breakIndex == 0) {
  46.         // Ignore break at the first character
  47.         messageText = messageText.slice(1);
  48.         messageFragment = "";
  49.       } else if (breakIndex > 0) {
  50.         // The fragment up to the break
  51.         messageFragment = messageText.slice(0, breakIndex);
  52.  
  53.         // Chop off fragment we just found from remaining string
  54.         messageText = messageText.slice(breakIndex+1);
  55.       } else {
  56.         // "\n" not found. We're done
  57.         done = true;
  58.         messageFragment = messageText;
  59.       }
  60.       messageParent.setAttribute("value", messageFragment);
  61.     }
  62.   }
  63.  
  64.   var windowTitle = param.GetString( 0 );
  65.   window.title = windowTitle;
  66.  
  67.   list = document.getElementById("list");
  68.   numItems = param.GetInt( 2 )
  69.  
  70.   for ( i = 2; i <= numItems+1; i++ ) {
  71.     var newString = param.GetString( i );
  72.     if (newString == "") {
  73.       newString = "<>";
  74.     }
  75.     elements[i-2] = AppendStringToTreelist(list, newString);
  76.   }
  77.   list.selectItem(elements[0]);
  78.  
  79.   // resize the window to the content
  80.   window.sizeToContent();
  81.  
  82.   // Move to the right location
  83.   moveToAlertPosition();
  84.   param.SetInt(0, 1 );
  85.   centerWindowOnScreen();
  86. }
  87.  
  88. function commonDialogOnOK() {
  89.   for (var i=0; i<numItems; i++) {
  90.     if (elements[i] == list.selectedItems[0]) {
  91.       param.SetInt(2, i );
  92.       break;
  93.     }
  94.   }
  95.   param.SetInt(0, 0 );
  96.   return true;
  97. }
  98.  
  99. function commonDialogOnCancel() {
  100.   for (var i=0; i<numItems; i++) {
  101.     if (elements[i]) {
  102.       param.SetInt(2, i );
  103.       break;
  104.     }
  105.   }
  106.   param.SetInt(0, 1 );
  107.   return true;
  108. }
  109.  
  110. // following routine should really be in a global utilities package
  111.  
  112. function AppendStringToTreelist(tree, string)
  113. {
  114.   if (tree)
  115.   {
  116.     var treechildren = document.getElementById('child');
  117.  
  118.     var treeitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treeitem");
  119.     var treerow = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treerow");
  120.     var treecell = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treecell");
  121.     if (treeitem && treerow && treecell)
  122.     {
  123.       treecell.setAttribute("label", string);
  124.       treerow.appendChild(treecell);
  125.       treeitem.appendChild(treerow);
  126.       treechildren.appendChild(treeitem)
  127.       var len = Number(tree.getAttribute("length"));
  128.       if (!len) len = -1;
  129.       tree.setAttribute("length",len+1);
  130.       return treeitem;
  131.     }
  132.   }
  133.   return null;
  134. }